<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- topic.qdoc -->
  <title>The QML 类型System | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td >
			QML 类型<span lang="zh-cn">系统</span></td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#basic-types">基本类型</a></li>
<li class="level1"><a href="#javascript-types">JavaScript <span lang="zh-cn">类型</span></a></li>
<li class="level1"><a href="#qml-object-types">QML<span lang="zh-cn">对象类型</span></a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML类型系统</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-typesystem-topic.html-description -->
<div class="descr"> <a name="details"></a>
<p>QML文档中对象层次结构定义中使用的类型可以来自不同的来源。<span lang="zh-cn">它们</span>可能是:</p>
<ul>
<li>由QML语言提供</li>
<li>通过C++通过QML模块注册</li>
<li>QML模块作为QML文档提供</li>
</ul>
<p>此外，应用程序开发人员可以提供他们自己的类型，可以直接注册C++类型，也可以在QML文档中定义可重用的组件，然后导入这些组件。</p>
<p>无论类型定义来自哪里，引擎都将对这些类型的属性和实例实施类型安全。</p>
<a name="basic-types"></a>
<h2 id="basic-types">基本类型</h2>
<p>QML语言内置了对各种基本类型的支持，包括整数、双精度浮点数、字符串和布尔值。对象可以具有这些类型的属性，这些类型的值可以作为参数传递给对象的方法。</p>
<p><span lang="zh-cn">请参考</span> <a href="qtqml-typesystem-basictypes.html">QML基本类型</a> 
文档了解更多信息。</p>
<a name="javascript-types"></a>
<h2 id="javascript-types">JavaScript<span lang="zh-cn">类型</span></h2>
<p>QML引擎支持JavaScript对象和数组。任何标准JavaScript类型都可以使用泛型var类型创建和存储。</p>
<p>例如，标准的Date和Array类型可用，如下所示:</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      property <span class="type"><a href="qml-var.html">var</a></span> <span class="name">theArray</span>: []
      property <span class="type"><a href="qml-var.html">var</a></span> <span class="name">theDate</span>: new <span class="name">Date</span>()

      <span class="name">Component</span>.onCompleted: {
          <span class="keyword">for</span> (var i = 0; <span class="name">i</span> <span class="operator">&lt;</span> <span class="number">10</span>; i++)
              <span class="name">theArray</span>.<span class="name">push</span>(<span class="string">&quot;Item &quot;</span> <span class="operator">+</span> <span class="name">i</span>)
          <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;There are&quot;</span>, <span class="name">theArray</span>.<span class="name">length</span>, <span class="string">&quot;items in the array&quot;</span>)
          <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;The time is&quot;</span>, <span class="name">theDate</span>.<span class="name">toUTCString</span>())
      }
  }

</pre>
<p>有关更多细节，请参阅<a href="qtqml-javascript-expressions.html">QML文档中的JavaScript表达式</a>。</p>
<a name="qml-object-types"></a>
<h2 id="qml-object-types">QML对象类型</h2>
<p>QML对象类型是可以实例化QML对象的类型。QML对象类型派生自<a href="qml-qtqml-qtobject.html">QtObject</a>，并由QML模块提供。应用程序可以导入这些模块来使用它们提供的对象类型。<code>QtQuick</code> 
模块提供了在QML中创建用户界面所需的最常见的对象类型。</p>
<p>最后，每个QML文档隐式地定义了一个QML对象类型，该对象类型可以在其他QML文档中重用。有关对象类型的详细信息，请参阅<a href="qtqml-typesystem-objecttypes.html">QML类型系统中关于对象类型</a>的文档。</p>
</div>
<!-- @@@qtqml-typesystem-topic.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
